public class InitiateFulfillmentOrder{

/* IDC 27/01/2011 for AAS
   Called from InitiateOrder from Opty page   Creates one Order per OpportunitylineItem 
   Marks the opty to show orders have been created 
   
   Michael Shu 6/2/2011
   - Fix the issues of "Initial Order Fulfillment" function in s139
   - Group opportunity line items by the products.
   - Simplify the interface of the method.   
*/
   //@future
   public static void CreateFulfillmentOrders(string id)     
    {
        List<Order__c> FulfillmentList = new List<Order__c>();

       List<Opportunitylineitem> OptyLine = [select id,TotalPrice,PriceBookEntry.Product2.ProductCode, PriceBookEntry.Product2.Name from Opportunitylineitem where OpportunityId = :id]; 
       Map<String, Decimal> lineItems = new Map<String, Decimal>();
       for(Opportunitylineitem item : OptyLine){
          String key = item.PriceBookEntry.Product2.ProductCode + ';' + item.PriceBookEntry.Product2.Name;
          if(lineItems.containsKey(key)){
            Decimal temp = lineItems.get(key);
            lineItems.put(key, temp + item.TotalPrice);
          }else{
            lineItems.put(key, item.TotalPrice);
          }
       }
       List<Quote> FinalQuote = [select id from quote where opportunityid =: id order by createddate desc limit 1];
       Opportunity opty = [select NPO_ID__c,order_initiated__c,Pre_Sale_Manager__c,Post_Sale_Manager__c, Contract_Start_Date__c, Contract_Termination_Date__c, Credit_Check_Info_Secured__c from opportunity where id =:id];
       opty.order_initiated__c = true; 
       for(String s : lineItems.keySet() ){
          String[] ss = s.split(';', 2);
          String pCode = ss[0];
          String pName = ss[1];
          Order__c fulfillmentorder = new Order__c();
          fulfillmentorder.Opportunity__c = id;
          if(FinalQuote.size() > 0 ){fulfillmentorder.Quote__c = FinalQuote[0].Id;}
          fulfillmentorder.Campaign_End_Date__c = opty.Contract_Termination_Date__c;
          fulfillmentorder.Campaign_Start_Date__c =  opty.Contract_Start_Date__c;
          fulfillmentorder.Contracted_Product_Amount__c = lineItems.get(s);
          fulfillmentorder.Ordered_Product_Amount__c = lineItems.get(s); 
          fulfillmentorder.Product__c = pName;
          fulfillmentorder.Unique_Product_ID__c = pCode;
          fulfillmentorder.Credit_Check_Info_Secured__c = opty.Credit_Check_Info_Secured__c;
          fulfillmentorder.Pre_Sale_Manager__c = opty.Pre_Sale_Manager__c ;
          fulfillmentorder.Post_Sale_Manager__c = opty.Post_Sale_Manager__c;
          //add by naidu and deactivate "AAS Update NAO ID" workflow.
          fulfillmentorder.Unique_Order_Id__c=opty.NPO_ID__c + ' - ' + pCode;
          FulfillmentList.add(fulfillmentorder); 
           system.debug('testingFulfillment'+fulfillmentorder);
       }
        system.debug('testingFulfillment'+FulfillmentList);
       Boolean rst = true;     
       try{
           insert FulfillmentList;
           FollowUpOrderHelper.setAlreadyCreatedFollowUpTasks();
           update opty;
           system.debug('testingFulfillmentlist'+FulfillmentList);
          
       }catch(Exception e){
           system.debug('Order Insertion Failed' + e);
           GeneralConstants.mailAnyExceptionToMe('InitiateFulfillmentOrder', 'AAS Init order fail', e);
           rst = false;
       }
       //return rst;
    }
    
}